package monster.com.cvh.activity;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputFilter;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import butterknife.BindView;
import butterknife.OnClick;
import monster.com.cvh.R;
import monster.com.cvh.activity.base.PermissionActivity;
import monster.com.cvh.bean.GetBindPhoneStatusBean;
import monster.com.cvh.bean.PostBean;
import monster.com.cvh.util.MyConstant;
import monster.com.cvh.util.ResgisterButton;
import monster.com.cvh.util.SPUtils;
import monster.com.cvh.util.StatusBarUtil;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by ASUS on 2018/3/14.
 */

public class PhoneBindActivity extends PermissionActivity {
    protected ProgressDialog mDialog;//网络请求ProgressDialog
    @BindView(R.id.iv_news_details_title_back)
    ImageView ivNewsDetailsTitleBack;
    @BindView(R.id.tv_activity_phone_bind_code)
    TextView tvActivityPhoneBindCode;
    @BindView(R.id.tv_activity_phone_bind_save)
    TextView tvActivityPhoneBindSave;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.details_title)
    RelativeLayout detailsTitle;
    @BindView(R.id.iv_phone)
    ImageView ivPhone;
    @BindView(R.id.edt_activity_setting_phone)
    EditText edtActivitySettingPhone;
    @BindView(R.id.iv_check)
    ImageView ivCheck;
    @BindView(R.id.edt_activity_register)
    EditText edtActivityRegister;
    @BindView(R.id.activity_phone_bind_tip)
    TextView activityPhoneBindTip;
    private String token;
    private Gson gson;
    private GetBindPhoneStatusBean getBindPhoneStatusBean;
    private String phone;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDialog = new ProgressDialog(PhoneBindActivity.this);
        mDialog.setMessage(getResources().getString(R.string.wait));
        mDialog.setCancelable(false);
        mDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                OkGo.getInstance().cancelTag(this);
            }
        });
        mDialog.setCanceledOnTouchOutside(false);
    }

    @Override
    protected int getContentViewResId() {
        return R.layout.activity_phone_bind;
    }

    @Override
    protected void initEvent() {
        activityPhoneBindTip.setVisibility(View.INVISIBLE);
        //设置标题
        tvTitle.setText("手机绑定");
        //限制手机号码字数为11位
        edtActivitySettingPhone.setFilters(new InputFilter[]{new InputFilter.LengthFilter(11)});
    }

    @Override
    protected void initData() {
        //修改状态栏颜色
        StatusBarUtil.setStatusBarLightMode(PhoneBindActivity.this, Color.WHITE);
        token = SPUtils.getString(PhoneBindActivity.this, "token", "");
        gson = new Gson();
        OkGo.<String>get(MyConstant.GET_BIND_pHONE_STATE).params("token", SPUtils.getString(PhoneBindActivity.this, "token", ""))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Gson gson = new Gson();
                        getBindPhoneStatusBean = gson.fromJson(s, GetBindPhoneStatusBean.class);
                        switch (getBindPhoneStatusBean.getCode()) {
                            case 1:
                                //获取成功
                                if (!getBindPhoneStatusBean.getData().equals("")) {
                                    edtActivitySettingPhone.setText(getBindPhoneStatusBean.getData());
                                    return;
                                }
                                break;
                        }
                    }
                });
        phone = getIntent().getStringExtra("phone");
    }


    /**
     * 后退
     */
    @OnClick(R.id.iv_news_details_title_back)
    public void onIvNewsDetailsTitleBackClicked() {
        finish();
    }

    /**
     * 获取验证码
     */
    @OnClick(R.id.tv_activity_phone_bind_code)
    public void onTvActivityPhoneBindCodeClicked() {
        //获取验证码
        if (edtActivitySettingPhone.getText().toString() == null || edtActivitySettingPhone.getText().toString().isEmpty()) {
            showToastLong("请输入手机号码");
        } else {
            ResgisterButton button = new ResgisterButton(tvActivityPhoneBindCode, 60000, 1000);
            button.start();
            OkGo.<String>post(MyConstant.SMS_CODE_URL).params("phone", edtActivitySettingPhone.getText().toString().trim()).execute(new StringCallback() {
                @Override
                public void onSuccess(String s, Call call, Response response) {
                    Gson gson = new Gson();
                    PostBean postBean = gson.fromJson(s, PostBean.class);
                    switch (postBean.getCode()) {
                        case 1:
                            //请求成功
                            showToastLong("获取验证码成功");
                            break;
                        case -1004:
                            showToastLong("请输入手机号码");
                            break;
                        case 10:
                            showToastLong("手机号码填写错误");
                            break;
                    }
                }
            });
        }
    }

    /**
     * 确定按钮
     */
    @OnClick(R.id.tv_activity_phone_bind_save)
    public void onTvActivityPhoneBindSaveClicked() {
        if (!getBindPhoneStatusBean.getData().equals("")) {
            if (getBindPhoneStatusBean.getData().equals(edtActivitySettingPhone.getText().toString().trim())) {
                activityPhoneBindTip.setVisibility(View.VISIBLE);
            } else {
                activityPhoneBindTip.setVisibility(View.INVISIBLE);
            }
            return;
        }
        mDialog.show();
        OkGo.<String>post(MyConstant.BIND_MOBILE)
                .tag(this)
                .params("token", token)
                .params("phone", edtActivitySettingPhone.getText().toString().trim())
                .params("verify_code", edtActivityRegister.getText().toString().trim())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            PostBean bean = gson.fromJson(s, PostBean.class);
                            switch (bean.getCode()) {
                                case 1:
                                    finish();
                                    showToastLong("绑定成功");
                                    break;
                                case -1002:
                                case -1003:
                                case -1004:
                                case -103:
                                    showToastLong("绑定失败");
                                    break;
                                case 10:
                                    showToastLong("无效的手机号呢");
                                    break;
                                case 19:
                                    showToastLong("验证码输入错误");
                                    break;
                                case -1005:
                                    showToastLong("验证码失效");
                                    break;
                                case -156:
                                    showToastLong("手机号已绑定其他账户");
                                    break;
                            }
                        } catch (JsonSyntaxException e) {
                            e.printStackTrace();
                            showToastShort(R.string.net_load_failed);
                        } finally {
                            mDialog.dismiss();
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        showToastShort(R.string.net_load_failed);
                        mDialog.dismiss();
                    }
                });
    }

}
